package org.jprobe.ui;

import java.awt.BorderLayout;
import java.awt.Cursor;

import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JPanel;

import org.jprobe.CoverageResults;
import org.jprobe.JUnitResults;
import org.jprobe.ProjectExplorer;
/**
 *
 * Results Viewer panel
 *
 * @author Hussain Al-Mutawa
 * @version 1.0
 * @since 1.0
 */
public class ResultsViewerPanel extends JPanel {
	/** serial version uid **/
	private static final long serialVersionUID = 4752289948656411269L;

	private transient final JEditorPane editPane;
	public ResultsViewerPanel() {
		super();
		this.editPane=new JEditorPane();
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
		this.add(editPane,BorderLayout.CENTER);
		this.editPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
		this.editPane.setEditable(false);
	}

	private transient static final String TD_START="<td>",TD_END="</td>";

	public void showResults(final ProjectExplorer projectExplorer){
		final CoverageResults coverage = projectExplorer.getCoverageResult();
		final JUnitResults junit = projectExplorer.getJUnitResults();

		final String cClass = TD_START + coverage.getClassCoverage() + TD_END;
		final String cMethod = TD_START +  coverage.getMethodCoverage() + TD_END;
		final String cBlock = TD_START +  coverage.getBlockCoverage() + TD_END;
		final String cLine = TD_START +  coverage.getLineCoverage() + TD_END;
		final String tTime = TD_START +  junit.getTestingTime() + TD_END;
		final String tErrors = TD_START +  junit.getErrors() + TD_END;
		final String tFailures = TD_START +  junit.getFailures() + TD_END;
		final String tSuccessRate = TD_START +  junit.getSuccessRate() + TD_END;
		final String tTests = TD_START +  junit.getTests() + TD_END;
		final String text =
			"<html><head><style>"+
			" td{border-width:1pt;border-style:solid;border-color:#cccccc;}"+
			" th{border-width:1pt;border-style:solid;border-color:#cccccc;}"+
			" td{text-align:center;white-space:nowrap;width:100px;}"+
			" td.imp{background-color:#FFFF99;}"+
			" td.red{color:#FF0000;}"+
			" body{background-color:#FFFFFF;}"+
			"</style></head><body><h1>"+projectExplorer.getProject()+"</h1>"+
			"	<table cellspacing='1' cellpadding='3' style='border-collapse:collapse;background-color:#ffffff'>"+
			"		<tr><th style='background-color:#33CCFF' colspan='4'>COVERAGE</th><th style='background-color:#00CC99' colspan='5'>TESTING</th></tr>"+
			"		<tr><td>Class</td><td>Method</td><td>Block</td><td>Line</td><td>Tests</td><td>Failures</td><td>Errors</td><td>Success Rate</td><td>Time</td></tr>"+
			"		<tr>" + cClass + cMethod + cBlock + cLine + tTests + tFailures + tErrors + tSuccessRate + tTime + "</tr>" +
			"	</table></body>" +
			"</html>";

		this.editPane.setContentType("text/html");
		this.editPane.setText(text);
	}

}
